package pos.dato;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import pos.dominio.Comentario;
import pos.dominio.Evento;
import pos.dominio.Usuario;

public class JDBCComentarioDAO implements IComentarioDAO{

     IUsuarioDAO udao;
     IEventoDAO edao;
    
    public JDBCComentarioDAO() {
        udao = new JDBCUsuarioDAO();
        edao= new JDBCEventoDAO();
    }
    public List<Comentario> selectAllComentarios() {
    	List<Comentario> comentarios=new LinkedList<Comentario>();
        String sql = "SELECT * FROM Comentarios";
        PreparedStatement stmt = null;
    	Connection conn = ConnectionManager.getInstance().checkOut();
       ResultSet result = null;

        try {
            stmt = conn.prepareStatement(sql);
            result = stmt.executeQuery();
            while (result.next()) {
                Comentario temp = new Comentario();
                temp.setComentarioID(result.getString("comentarioID"));
                temp.setOpinion(result.getString("opinion"));
                temp.setUsuario(result.getString("usuarioID"));
                temp.setEventoID(result.getString("eventoID"));
                comentarios.add(temp);
            }
            
 
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return comentarios;
    }
    public void insert(Comentario c){
    	Connection conn = ConnectionManager.getInstance().checkOut();
        
    	PreparedStatement stmt = null;
        String sql = "INSERT INTO Comentarios( comentarioID,usuarioID,opinion, eventoID) VALUES ( ?, ?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, c.getComentarioID());
            stmt.setString(2,c.getUsuario());
            stmt.setString(3, c.getOpinion());
            stmt.setString(4, c.getEventoID());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
    }

}
